//
//  BWVisitorLoginView.swift
//  weibo
//
//  Created by libowen on 16/5/21.
//  Copyright © 2016年 libowen. All rights reserved.
//

import UIKit
import SnapKit

//@objc protocol BWVisitorLoginViewDelegate: NSObjectProtocol {
//    optional func userWilllogin()
//    func userWiilRegister()
//}

class BWVisitorLoginView: UIView {
//    weak var visitorDelegate: BWVisitorLoginViewDelegate?
    
    var userWilllogin: (()->())?
    var userWiilRegister: (()->())?
    
    func updateUI(title: String,imageName: String,isHome: Bool = false) {
        if isHome {
            startAnimation()
        } else {
            largeHouse.hidden = true
            bringSubviewToFront(circleIcon)
        }
        circleIcon.image = UIImage(named: imageName)
        tipLable.text = title
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    private func startAnimation() {
        let basic = CABasicAnimation(keyPath: "transform.rotation")
        basic.duration = 15
        basic.repeatCount = MAXFLOAT
        basic.toValue = 2 * M_PI
        basic.removedOnCompletion = false
        circleIcon.layer.addAnimation(basic, forKey: nil)
    }
    private func setupUI() {
        addSubview(circleIcon)
        addSubview(backIcon)
        addSubview(largeHouse)
        addSubview(tipLable)
        addSubview(loginBtn)
        addSubview(registerBtn)
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        
        largeHouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        tipLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.centerX.equalTo(circleIcon.snp_centerX)
            make.height.equalTo(40)
        }
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLable.snp_left)
            make.top.equalTo(tipLable.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        registerBtn.snp_makeConstraints { (make) in
            make.right.equalTo(tipLable.snp_right)
            make.top.equalTo(tipLable.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        backIcon.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        let value: CGFloat = 237.0 / 255.0
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
        
        loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
    }
    
    @objc private func loginBtnDidClick() {
//        visitorDelegate?.userWilllogin?()
        userWilllogin?()
    }
    @objc private func registerBtnDidClick() {
//        visitorDelegate?.userWiilRegister()
        userWiilRegister?()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   //MARK: 懒加载子控件
    private lazy var backIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var largeHouse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var tipLable: UILabel = {
        let l = UILabel()
        l.text = "关注一些人,回到这里看看有什么惊喜"
        l.textColor = UIColor.darkGrayColor()
        l.font = UIFont.systemFontOfSize(14)
        l.textAlignment = .Center
        l.numberOfLines = 0
        l.preferredMaxLayoutWidth = 230
        l.sizeToFit()
        return l
    }()
    
    private lazy var loginBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        let image = UIImage(named: "common_button_white_disable")!
        let capW = Int(image.size.width * 0.5)
        let capH = Int(image.size.height * 0.5)
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
        return btn
    }()
    private lazy var registerBtn: UIButton = {
        let btn = UIButton()
        
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        //设置文字大小
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        let image = UIImage(named: "common_button_white_disable")!
        let capW = Int(image.size.width * 0.5)
        let capH = Int(image.size.height * 0.5)
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
        return btn
    }()
    
}


































